package org.example.book.bridge;

import org.example.book.pojo.UserInfo;
import org.example.book.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 默认登录
 * @author jeff
 */
@Component
public class RegisterLoginByDefault extends AbstractRegisterLoginFunc implements RegisterLoginFuncInterface{

    @Autowired
    private UserRepository userRepository;

    @Override
    public String login(String username, String password) {
//        UserInfo byUserName = userRepository.findByUserNameAndUserPassword(username, password);
//        if (byUserName == null) {
//            throw new RuntimeException("username or password incorrect");
//        }
//        return "login success";

        try {
            return super.commonLogin(username, password, userRepository);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String register(UserInfo userInfo) {
//        if (checkExists(userInfo.getUserName())) {
//            throw new RuntimeException("user already exists");
//        }
//        userInfo.setCreateDate(new Date());
//        userRepository.save(userInfo);
//        return "register success";
        return super.commonRegister(userInfo, userRepository);
    }

    @Override
    public boolean checkExists(String username) {
//        UserInfo userInfo = userRepository.findByUserName(username);
//        return userInfo != null;
        return super.checkExists(username, userRepository);
    }
}
